package com.store.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.OneYuanBuyBean;

import java.util.List;
import java.util.Map;

public class OneYuanBuyAdapter extends BaseAdapter {
    private List<OneYuanBuyBean> goodsList;
    private Map<String,String> map;
    private Context context;


    public OneYuanBuyAdapter(List<OneYuanBuyBean> goodsList,Map<String,String> map, Context context) {
        this.goodsList = goodsList;
        this.map=map;
        this.context = context;
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int position) {

        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_oneyuanbuy, null);
            viewHolder.goods_img = (ImageView) convertView.findViewById(R.id.oyb_item_img);
            viewHolder.goods_title = (TextView) convertView.findViewById(R.id.oyb_item_title);
            viewHolder.goods_time = (TextView) convertView.findViewById(R.id.oyb_item_time);
            viewHolder.tv_activity_type = (TextView) convertView.findViewById(R.id.tv_activity_type);
            viewHolder.img_red = (ImageView) convertView.findViewById(R.id.oyb_item_img_red);
            viewHolder.img_gray = (ImageView) convertView.findViewById(R.id.oyb_item_img_gray);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String title= goodsList.get(position).getTitle();
        String award_name= goodsList.get(position).getAward_name();
//        String s_name=title+"("+award_name+")";
        viewHolder.goods_time.setText(award_name);
        String activity_type = goodsList.get(position).getActivity_type();

        if(activity_type.equals("DSK")){
            viewHolder.tv_activity_type.setText("定时开");
        }else if(activity_type.equals("YYY")){
            viewHolder.tv_activity_type.setText("摇一摇");
        }else if(activity_type.equals("GGL")){
            viewHolder.tv_activity_type.setText("刮刮乐");
        }else{
            viewHolder.tv_activity_type.setText("定时开");
        }
        viewHolder.goods_title.setText(title);

//        //商品名
//        if (!TextUtils.isEmpty(s_name)) {
//            viewHolder.goods_title.setText(s_name);
//        }

       //时间
//        String s_end_date = goodsList.get(position).getEnd_date();
//        if (!TextUtils.isEmpty(s_end_date)) {
//            viewHolder.goods_time.setText("截止时间:"+s_end_date);
//        }
        String url=null;
        if(map!=null){
            String s_Award_pic_path1 = goodsList.get(position).getAward_pic_path1();
            url=map.get(s_Award_pic_path1);
        }
        ImageLoader.getInstance().displayImage(url, viewHolder.goods_img, options);



      /*  一元购活动状态 进行中
        public static final String ACTIVITIES_STATUS_PROCESSING = "processing";
        一元购活动状态 已揭晓
        public static final String ACTIVITIES_STATUS_ANNOUNCED = "announced";
        一元购活动状态 已取消
        public static final String ACTIVITIES_STATUS_CANCEL = "cancel";
        一元购活动状态 未达成
        public static final String ACTIVITIES_STATUS_UNREACHED = "unreached";
        一元购活动状态 已兑换
        public static final String ACTIVITIES_STATUS_EXCHANGED = "exchanged";
        一元购活动状态 已结束(兼容老数据)
        public static final String ACTIVITIES_STATUS_FINISHED = "finished";*/

        String s_Status = goodsList.get(position).getStatus();
        if(s_Status.equals("processing")){
            setItemView(viewHolder,R.drawable.oyb_tiem_jxz);
        }else if(s_Status.equals("cancel")){   //已经取消
            setLuckItemView(viewHolder,R.drawable.oyb_tiem_cancel);
        }else if(s_Status.equals("exchanged")){
            setLuckItemView(viewHolder,R.drawable.oyb_tiem_ylq);
        }else if(s_Status.equals("announced")){
            setLuckItemView(viewHolder,R.drawable.oyb_tiem_yjx);

        }else if(s_Status.equals("unreached")){
            setLuckItemView(viewHolder,R.drawable.oyb_tiem_wdc);
        }else if(s_Status.equals("finished")){
            if(TextUtils.isEmpty(goodsList.get(position).getLuck_code())){
                setLuckItemView(viewHolder,R.drawable.oyb_tiem_yjs);
            }else {
                setLuckItemView(viewHolder,R.drawable.oyb_tiem_yjs);
            }
        }

        return convertView;
    }

    private void setItemView(ViewHolder viewHolder, int pic) {
        viewHolder.img_red.setVisibility(View.VISIBLE);
        viewHolder.img_red.setBackgroundResource(pic);
        viewHolder.img_gray.setVisibility(View.GONE);

    }

    private void setLuckItemView(ViewHolder viewHolder,int pic) {
        viewHolder.img_red.setVisibility(View.GONE);
        viewHolder.img_gray.setVisibility(View.VISIBLE);
        viewHolder.img_gray.setBackgroundResource(pic);
    }


    class ViewHolder {
        private ImageView goods_img;
        private TextView goods_title;
        private TextView goods_time;
        private TextView tv_activity_type;
        private ImageView img_red;
        private ImageView img_gray;
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.default_picture_s)
            .showImageOnFail(R.drawable.default_picture_s)
            .showStubImage(R.drawable.default_picture_s)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}
